//
//  MineFavoriteVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "MineFavoriteVC.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "MineFavoriteTableCell.h"
#import "MineFavoriteDetailsVC.h"

@interface MineFavoriteVC ()
@property(nonatomic, retain) NSMutableArray *arrayActivity;
@property(nonatomic, retain) NSMutableArray *array;
@property(nonatomic, retain) UITableView *myTableV;
@property(nonatomic, retain) DiscountListModel *model;
@end

@implementation MineFavoriteVC

- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"userName"];
    [self.myTableV reloadData];
    if (userName == nil) {
        self.array = nil;
    }else{
        self.array = [NSMutableArray arrayWithArray:[[DataBaseHandle shareDataBaseHandle] selectDiscount:userName withSQname:@"discount"]];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    view.backgroundColor = [UIColor colorWithRed:53 / 255.0 green:176 /255.0 blue:131 / 255.0 alpha:1.0];
    [self.view addSubview:view];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 40, 25, 80, 30)];
    title.text = @"我 的";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [self.view addSubview:title];
  
    //data
     self.myTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 60)];
    [self.view addSubview:self.myTableV];
    
    //成为代理人
    self.myTableV.delegate = self;
    self.myTableV.dataSource = self;
    //注册
    UINib *nib = [UINib nibWithNibName:@"MineFavoriteTableCell" bundle:[NSBundle mainBundle]];
    [self.myTableV registerNib:nib forCellReuseIdentifier:@"reuse"];
    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 30, 20, 20);
    [button setImage:[UIImage imageNamed:@"icon-fanhui 64x64"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}
//返回按钮点击方法
- (void)buttonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

//建行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineFavoriteTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    DiscountListModel *model = [self.array objectAtIndex:indexPath.row];
    cell.myMlodel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//编辑
//1.设置哪些行为可编辑 YES 所有的都可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//2.设置为删除模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

//3.左滑动删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    DiscountListModel *model = [self.array objectAtIndex:indexPath.row];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"userName"];

     [[DataBaseHandle shareDataBaseHandle] deleteDiscountWithID:model.discountId userName:userName];
    [self.array removeObjectAtIndex:indexPath.row];
    [self.myTableV reloadData];
}

//触发方法, 进入详情页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineFavoriteDetailsVC *dataVC = [[MineFavoriteDetailsVC alloc] init];
    DiscountListModel *model = [self.array objectAtIndex:indexPath.row];
    dataVC.discountId = model.discountId;
    [self presentViewController:dataVC animated:YES completion:^{
        
    }];
 }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
